import unittest

from api import manager

import mysql.mysql_client

import mysql.user_mysql

which_id = {}


class TestManager(unittest.TestCase):
    mysql = None

    @classmethod
    def setUpClass(cls):
        cls.manager = manager.ManagerLitemall()
        cls.mysql = mysql.mysql_client.MySQLClient()
        cls.mysql.execute(mysql.user_mysql.del_sql)

    @classmethod
    def tearDownClass(cls):
        cls.mysql.close()

    def test_add(self):
        result = self.manager.add_manager('admin117', 'admin123')
        global which_id
        which_id.update({result.get('data').get('username'): result.get('data').get('id')})
        self.assertEqual(0, result.get('errno'))
        self.assertEqual('admin117', result.get('data').get('username'))
        self.assertEqual('成功', result.get('errmsg'))

    def test_mod(self):
        result = self.manager.mod_manager(which_id.get('admin117'), 'admin1000', 'admin321', {"roleIds": [1, 2]})
        self.assertEqual(0, result.get('errno'))
        self.assertEqual('admin1000', result.get('data').get('username'))
        self.assertEqual([1, 2], result.get('data').get('roleIds'))
        self.assertEqual('成功', result.get('errmsg'))

    def test_del(self):
        result = self.manager.del_manager(which_id.get('admin117'), 'admin1000', 'admin321', {"roleIds": [1, 2]})
        self.assertEqual(0, result.get('errno'))
        self.assertEqual('成功', result.get('errmsg'))

    def test_sea(self):
        result = self.manager.sea_manager()
        self.assertEqual(0, result.get('errno'))
        self.assertEqual(3, result.get('data').get('total'))
        self.assertEqual('成功', result.get('errmsg'))
